package com.example.inperson;

import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;

public class ProfileScreen extends ActionBarActivity {	
	
	//wat
	private static int RESULT_LOAD_IMAGE = 1;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_profile_screen);

		if (savedInstanceState == null) {
			getSupportFragmentManager().beginTransaction()
			.add(R.id.container, new PlaceholderFragment()).commit();
		}
		
	}//end of onCreate
	


	// Called when the next button is clicked.
	public void nextScreen(View view) {		
		//now get the information needed to save to the user profile
		boolean check1, check2, check3;
		String name;
		check1 = ((CheckBox) findViewById(R.id.checkBox1)).isChecked();
		check2 = ((CheckBox) findViewById(R.id.checkBox2)).isChecked();
		check3 = ((CheckBox) findViewById(R.id.checkBox3)).isChecked();
		name = ((EditText) findViewById(R.id.editText1)).getText().toString();

		Intent myIntent = new Intent(getBaseContext(), TasteProfile.class);
		myIntent.putExtra("name", name); //Optional parameters
		myIntent.putExtra("check1", check1);
		myIntent.putExtra("check2", check2);
		myIntent.putExtra("check3", check3);
		ProfileScreen.this.startActivity(myIntent);	
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {

		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.profile_screen, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}
	
	//no longer references the compass, but instead the profile image which can be changed
	public void onCompass(View view) {
		Intent i = new Intent(
				Intent.ACTION_PICK,
				android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
		
		startActivityForResult(i, RESULT_LOAD_IMAGE);
	}
	
	//related to onCompass to select a new image
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
         
        if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) {
            Uri selectedImage = data.getData();
            String[] filePathColumn = { MediaStore.Images.Media.DATA };
 
            Cursor cursor = getContentResolver().query(selectedImage,
                    filePathColumn, null, null, null);
            cursor.moveToFirst();
 
            int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
            String picturePath = cursor.getString(columnIndex);
            cursor.close();
             
            Bitmap yourBitmap = BitmapFactory.decodeFile(picturePath);
            Bitmap resized = Bitmap.createScaledBitmap(yourBitmap,(int)(yourBitmap.getWidth()*0.4), (int)(yourBitmap.getHeight()*0.4), true);
            ImageView imageView = (ImageView) findViewById(R.id.imageButton1);
            imageView.setImageBitmap(resized);
         
        }
     
     
    }	
	
	
	
	
	// BUG: When invoked, this function properly clears the text, but then does not allow the user to break focus on the text field.
	public void clearNameBoxIfNecessary(View view) {
		if (((EditText) findViewById(R.id.editText1)).getText().toString().equalsIgnoreCase("Your First Name")) {
			Log.d("tag","Clearing name field.");
			((EditText) findViewById(R.id.editText1)).setText("");
		}
	}

	/**
	 * A placeholder fragment containing a simple view.
	 */
	public static class PlaceholderFragment extends Fragment {

		public PlaceholderFragment() {
		}

		@Override
		public View onCreateView(LayoutInflater inflater, ViewGroup container,
				Bundle savedInstanceState) {
			View rootView = inflater.inflate(R.layout.fragment_profile_screen,
					container, false);			
			return rootView;
		}
	}

}
